package view;

import controller.AddUserController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class AddUserView {
    private final String username;
    private final AddUserController addUserController = new AddUserController();

    public AddUserView(String username) {
        this.username = username;
    }

    public void showUi() {
        JFrame jf = new JFrame("添加好友");
        jf.setIconImage(Toolkit.getDefaultToolkit().createImage(MainView.class.getResource("qq.jpg")));
        JPanel jp = new JPanel();
        JLabel jl1 = new JLabel("添加好友");
        JLabel jLabel = new JLabel("请输入需添加好友的账号昵称");
        JTextField textField = new JTextField(10);
        jLabel.setFont(new Font("黑体", Font.BOLD, 24));
        jl1.setFont(new Font("黑体", Font.BOLD, 24));
        JButton bu2 = new JButton("添    加");
        JButton bu3 = new JButton("退    出");
        textField.setFont(new Font("黑体", Font.BOLD, 24));
        jp.setLayout(null);
        jl1.setBounds(175, 1, 200, 50);
        jLabel.setBounds(50, 50, 400, 50);
        textField.setBounds(50, 100, 400, 50);
        bu2.setBounds(175, 200, 100, 50);
        bu3.setBounds(175, 300, 100, 50);
        jf.getRootPane().setDefaultButton(bu2);
        jp.add(jl1);
        jp.add(textField);
        jp.add(jLabel);
        jp.add(bu2);
        jp.add(bu3);
        jf.add(jp);
        jf.setVisible(true);
        jf.setBounds(600, 200, 500, 500);


        bu2.addActionListener(e -> {
            String username2 = textField.getText();
            if (!username.equals(username2)) {
                int i = addUserController.addUser(username, username2);
                if (i == 1) {
                    textField.setText("");
                    JOptionPane.showMessageDialog(null, "添加成功！！！");
                } else if (i == 2) {
                    JOptionPane.showMessageDialog(null, "当前用户不存在！！！");
                } else if (i == 3) {
                    JOptionPane.showMessageDialog(null, "添加用户已是好友！！！");
                } else if (i == 4) {
                    JOptionPane.showMessageDialog(null, "添加用户不存在！！！");
                }
            } else {
                JOptionPane.showMessageDialog(null, "无法添加自己为好友！！！");
            }
        });
        bu3.addActionListener(e -> jf.dispose());
        jf.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                jf.dispose();
            }
        });
    }
}